﻿﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NewLife.CommonEntity;
using NewLife.Web;
using XControl;
using System.Xml;
using System.Xml.Serialization;
using NewsPub;
using NewLife;
using NewLife.Security;
using XCode;

public partial class Pages_DetailsForm : MyEntityForm<Details>
{
    private Boolean isAdd;
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        EntityForm.OnSetForm += EntityForm_OnSetForm;
        //EntityForm.OnGetForm += EntityForm_OnGetForm;
        EntityForm.OnSaveSuccess += EntityForm_OnSaveSuccess;
        //EntityForm.OnSetForm += new EventHandler<EventArgs<IEntity>>(EntityForm_OnSetForm);
        //EntityForm.OnGetForm += new EventHandler<EventArgs<IEntity>>(EntityForm_OnGetForm);
        //EntityForm.OnSaveSuccess += new EventHandler<EventArgs<IEntity>>(EntityForm_OnSaveSuccess);
    }

    void EntityForm_OnSetForm(object sender, EntityFormEventArgs e)
    {
        //判断插入还是更新
        if (Entity.ID > 0) { frmEditTime.Text = DateTime.Now.ToString(); isAdd = false; } 
        else 
        {frmCreateTime.Text= frmEditTime.Text = DateTime.Now.ToString(); 
            frmUserID.Text = Manager.Current.ID.ToString();
            isAdd = true;
        }
        foreach (Classis c in Classis.FindAllChildsByParent(0))
        {
            if (c.Childs.Count > 0 || c.Name == "首页")
            {
                ListItem i = frmClassisID.Items.FindByValue(c.ID.ToString());

                i.Attributes.Add("style", "color:gray;");

                i.Attributes.Add("disabled", "true");
            }
        }
        //ListItem i = frmClassisID.Items.FindByValue("0");

        //    i.Attributes.Add("style", "color:gray;");

        //    i.Attributes.Add("disabled", "true");

        //    i.Value = "-1";
    }

    void EntityForm_OnGetForm(object sender, EntityFormEventArgs e)
    {
        //判断插入还是更新
        //if (isAdd)
        //{
            //加入更新sitemap.xml
            writemap();
        //    XmlDocument xmlDoc = new XmlDocument();

        //    xmlDoc.Load(Server.MapPath("~/sitemap.xml"));
        //    XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
        ////nsMgr.AddNamespace("ns", "http://www.sitemaps.org/schemas/sitemap/0.9");
        ////XmlNode root = xmlDoc.SelectSingleNode("urlset");
        //XmlNode root = xmlDoc.DocumentElement;
        //    XmlElement url = xmlDoc.CreateElement("url");
        //    url.RemoveAttribute("NamespaceURI");
        //    XmlElement loc = xmlDoc.CreateElement("loc");
        //    loc.InnerText = "网址";
        //    XmlElement priority = xmlDoc.CreateElement("priority");
        //    priority.InnerText = "1.00";
        //    XmlElement lastmod = xmlDoc.CreateElement("lastmod");
        //    lastmod.InnerText = DateTime.Today.ToShortDateString();
        //    XmlElement changefreq = xmlDoc.CreateElement("changefreq");
        //    changefreq.InnerText = "weekly";
        //    url.AppendChild(loc);
        //    url.AppendChild(priority);
        //    url.AppendChild(lastmod);
        //    url.AppendChild(changefreq);

        //    root.AppendChild(url);
        //    xmlDoc.Save(Server.MapPath("~/sitemap.xml"));
        //} 
    }
    void EntityForm_OnSaveSuccess(object sender, EntityFormEventArgs e)
    {
        //判断插入还是更新
        //if (isAdd == true)
        //{
            writemap();
        //加入更新sitemap.xml
        //XmlDocument xmlDoc = new XmlDocument();

        //xmlDoc.Load(Server.MapPath("~/sitemap.xml"));
        //XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
        ////nsMgr.AddNamespace("ns", "http://www.sitemaps.org/schemas/sitemap/0.9");
        ////XmlNode root = xmlDoc.SelectSingleNode("urlset");
        //XmlNode root = xmlDoc.DocumentElement;
        //XmlElement url = xmlDoc.CreateElement("url");
        //url.RemoveAttribute("NamespaceURI");
        //XmlElement loc = xmlDoc.CreateElement("loc");
        //loc.InnerText = "网址";
        //XmlElement priority = xmlDoc.CreateElement("priority");
        //priority.InnerText = "1.00";
        //XmlElement lastmod = xmlDoc.CreateElement("lastmod");
        //lastmod.InnerText = DateTime.Today.ToShortDateString();
        //XmlElement changefreq = xmlDoc.CreateElement("changefreq");
        //changefreq.InnerText = "weekly";
        //url.AppendChild(loc);
        //url.AppendChild(priority);
        //url.AppendChild(lastmod);
        //url.AppendChild(changefreq);

        //root.AppendChild(url);
        //xmlDoc.Save(Server.MapPath("~/sitemap.xml"));
        //} 
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //DataBind();

            //frmClassisID.Items.Add(new ListItem("|-请选择", "0"));
            //List<Classis> list = Classis.FindAll("select * from classis where name<>'首页' order by parentid,sort ");
            //if (list != null && list.Count > 0)
            //{
            //    foreach (Classis item in list)
            //    {
            //        String spaces = new String('　', item.Deepth);
            //        if (item.Childs.Count == 0) 
            //        frmClassisID.Items.Add(new ListItem(spaces + "|- " + item.Name, item.ID.ToString()));
            //    }
            //}
            //List<CustomerType> list = CustomerType.FindAll(CustomerType._.ParentID,0);
            //List<CustomerType> list = CustomerType.FindAll();
            //if (list != null && list.Count > 0)
            //{
            //    foreach (CustomerType item in list)
            //    {
            //        String spaces = new String('　', item.Deepth);
            //        typeList.Items.Add(new ListItem(spaces + "|- " + item.Name, item.ID.ToString()));
            //    }
            //}
            if (Entity.ID >0) frmClassisID.SelectedValue = Entity.Classis.ID.ToString();

            // 添加/编辑 按钮需要添加/编辑权限
            //if (EntityID > 0)
            //    UpdateButton.Visible = Acquire(PermissionFlags.Update);
            //else
            //    UpdateButton.Visible = Acquire(PermissionFlags.Insert);


        }
    }
    protected void writemap()
    {
        string fullFileName = "http://www.fjyxepb.gov.cn";
        string strLoc;
        string siteMapPath = Server.MapPath("~/sitemap.xml");

        FileInfo XMLFile = null;
        StreamWriter writerXMLFile = null;
        XMLFile = new FileInfo(siteMapPath);

        writerXMLFile = XMLFile.CreateText();
        //添加sitMap的头 
        writerXMLFile.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        writerXMLFile.WriteLine("<urlset");
        writerXMLFile.WriteLine("xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"");
        writerXMLFile.WriteLine("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
        writerXMLFile.WriteLine("xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9");
        writerXMLFile.WriteLine("http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">");
        //数据 
        //首页
        writerXMLFile.WriteLine("<url>");
        writerXMLFile.WriteLine("<loc>" + fullFileName + "</loc>");
        writerXMLFile.WriteLine("<priority>" + (1.0).ToString("0.00") + "</priority>");
        writerXMLFile.WriteLine("<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>");
        writerXMLFile.WriteLine("<changefreq>daily</changefreq>");
        writerXMLFile.WriteLine("</url>");
        //分类

        foreach (Classis news in Classis.FindAllByParentID(0))
        {
            strLoc = fullFileName + "/list.aspx?id=" + news.ID;
            writerXMLFile.WriteLine("<url>");
            writerXMLFile.WriteLine("<loc>" + strLoc + "</loc>");
            writerXMLFile.WriteLine("<priority>" + 0.6 + "</priority>");
            writerXMLFile.WriteLine("<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>");
            writerXMLFile.WriteLine("<changefreq>weekly</changefreq>");
            writerXMLFile.WriteLine("</url>");
        }
        //记录

        foreach (Details news in Details.FindAll())
        {
            strLoc = fullFileName + "/disp.aspx?id=" + news.ID;
            writerXMLFile.WriteLine("<url>");
            writerXMLFile.WriteLine("<loc>" + strLoc + "</loc>");
            writerXMLFile.WriteLine("<lastmod>" + news.EditTime.ToString("yyyy-MM-dd") + "</lastmod>");
            writerXMLFile.WriteLine("<changefreq>weekly</changefreq>");
            writerXMLFile.WriteLine("<priority>" + 0.5 + "</priority>");
            writerXMLFile.WriteLine("</url>");
        }
        //添加sitMap的尾 

        //千万不能忘了这句 
        writerXMLFile.WriteLine("</urlset>");

        writerXMLFile.Close();
        writerXMLFile.Dispose();
    }
}